<template>
  <div>
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入学号"
      v-model="username"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入岗位名称"
      v-model="position"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入公司名称"
      v-model="enterprise"
    />
    <el-select
      v-model="status"
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      clearable
      placeholder="请选择状态"
    >
      <el-option
        v-for="item in options"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      style="float: left; margin-left: 15px"
      size="small"
      @click="initData"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-button
      type="warning"
      plain
      size="small"
      icon="el-icon-upload2"
      style="float: left; margin-left: 15px; margin-bottom: 15px"
      @click="exportDelivery"
      >导出</el-button
    >
    <el-button
      style="float: left; margin-left: 15px"
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column
        prop="resume.sid"
        label="学号"
        width="300"
      ></el-table-column>
      <el-table-column
        prop="enterprise.nickname"
        label="公司名称"
        width="300"
      ></el-table-column>
      <el-table-column prop="recruit.name" label="岗位名称" width="300">
      </el-table-column>
      <el-table-column prop="status" label="投递状态" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">审核</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="编辑投递简历"
      :visible.sync="editDialogVisible"
      width="400px"
      center
    >
      <el-form :model="editData" ref="editForm" label-width="80px">
        <el-form-item prop="status" label="教室状态">
          <el-select
            style="width: 270px"
            v-model="editData.status"
            clearable
            placeholder="请选择教室状态"
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" type="primary" @click="updateDelivery"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      editDialogVisible: false,
      position: "",
      enterprise: "",
      status: "",
      options: ["投递中", "待查看", "已查看"],
      tableData: [],
      multipleSelection: [],
      total: 0,
      currentPage: 1,
      pageSize: 8,
      editData: {
        id: "",
        status: "",
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    exportDelivery() {
      this.$downloadReq("/deliver-resume/");
    },
    handleEdit(row) {
      this.editData = { id: row.id, status: row.status };
      this.editDialogVisible = true;
    },
    updateDelivery() {
      this.$put("deliver-resume/", this.editData).then((res) => {
        if (res.code == 200) {
          this.initData();
          this.editDialogVisible = false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm(
        '此操作将删除学号为"' + row.resume.sid + '"的数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        if (row.status == "已查看") {
          this.$delete("deliver-resume/?ids=" + row.id).then((res) => {
            if (res.code == 200) {
              this.initData();
            }
          });
        } else {
          this.$message.error("只能删除投递状态为已查看的数据");
        }
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个职位, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let ids = "?";
          this.multipleSelection.forEach((item) => {
            if (item.status != "已查看") {
              this.$message.error("所选数据中有审核状态不为已查看的数据");
              throw Error();
            } else {
              ids += "ids=" + item.id + "&";
            }
          });
          this.$delete("deliver-resume/" + ids).then((res) => {
            if (res.code == 200) {
              this.initData();
            }
          });
        })
        .catch(() => {
          this.initData();
        });
    },
    initData() {
      this.$get(
        "deliver-resume/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?sid=" +
          this.username +
          "&nickname=" +
          this.enterprise +
          "&name=" +
          this.position +
          "&status=" +
          this.status
      ).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    clearInput() {
      location.reload();
    },
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initData();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>

<style>
</style>